using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using SilenthinkContract.Component;

namespace SilenthinkContract.Domain
{
    /// <summary>
    /// 商品变体
    /// </summary>
    [Serializable]
    public class ProductVariant : BaseEntity<Guid>
    {
        /// <summary>
        /// 商品Id
        /// </summary>
        public int ProductId { get; set; }

        /// <summary>
        /// 商品变体编码
        /// </summary>
        public string VariantCode { get; set; }

        /// <summary>
        /// 商品变体价格
        /// </summary>
        public decimal VariantPrice { get; set; }

        /// <summary>
        /// 商品变体库存
        /// </summary>
        public int VariantQuantity { get; set; }

        /// <summary>
        /// 可用商品变体销售属性(xml)
        /// </summary>
        public string AvailableSaleXmlAttribute { get; set; }

        public string VariantMessage { get; set; }

        public IList<ProductAttributeInfo> AvailableSaleAttributeInfo
        {
            get
            {
                IList<ProductAttributeInfo> info = new List<ProductAttributeInfo>();
                if (!string.IsNullOrEmpty(AvailableSaleXmlAttribute))
                {
                    try
                    {
                        var xElement = XElement.Parse(AvailableSaleXmlAttribute);

                        info = xElement.Elements("attribute").Select(o => new ProductAttributeInfo
                        {
                            NameCode = o.Attribute("nameCode").Value,
                            Name = o.Attribute("name").Value,
                            ValueCode = o.Attribute("valueCode").Value,
                            Value = o.Attribute("value").Value,
                        }).ToList();
                    }
                    catch (Exception)
                    {
                        VariantMessage = "解析属性出错";
                    }
                }
                return info;
            }
        }
        //public virtual Product Product { get; set; }
        /*
        /// <summary>
        /// GTIN
        /// </summary>
        public string Gtin { get; set; }

        #region 礼品卡
        /// <summary>
        /// 是否为礼品卡
        /// </summary>
        public bool IsGiftCard { get; set; }

        /// <summary>
        /// 礼品卡类型
        /// </summary>
        public virtual GiftCardType GiftCardType { get; set; }
        #endregion

        #region 依赖
        /// <summary>
        /// 添加到购物车时是否依赖于其它产品变体
        /// </summary>
        public bool RequireOtherProducts { get; set; }

        /// <summary>
        /// 添加到购物车时是依赖于其它产品变体的Id(用逗号分隔)
        /// </summary>
        public string RequiredProductVariantIds { get; set; }

        /// <summary>
        /// 如果该产品依赖于其它产品变体,当加入购物车时是否自动添加相关依赖产品变体
        /// </summary>
        public bool AutomaticallyAddRequiredProductVariants { get; set; }
        #endregion

        #region 数字可下载商品
        /// <summary>
        /// 是否为下载产品
        /// </summary>
        public bool IsDownload { get; set; }

        /// <summary>
        /// 下载Id
        /// </summary>
        public int DownloadId { get; set; }

        /// <summary>
        /// 是否可无限次下载
        /// </summary>
        public bool UnlimitedDownloads { get; set; }

        /// <summary>
        /// 最大下载次数
        /// </summary>
        public int MaxNumberOfDownloads { get; set; }

        /// <summary>
        /// 用户下载有效期
        /// </summary>
        public int? DownloadExpirationDays { get; set; }

        /// <summary>
        /// 下载激活方式
        /// </summary>
        public DownloadActivationType DownloadActivationTypeId { get; set; }

        /// <summary>
        /// 是否有下载样品
        /// </summary>
        public bool HasSampleDownload { get; set; }

        /// <summary>
        /// 下载样本Id
        /// </summary>
        public int SampleDownloadId { get; set; }

        /// <summary>
        /// 是否包含用户协议
        /// </summary>
        public bool HasUserAgreement { get; set; }

        /// <summary>
        /// 用户协议
        /// </summary>
        public string UserAgreementText { get; set; }
        #endregion

        #region 分期付款
        /// <summary>
        /// 是否可分期付款
        /// </summary>
        public bool IsRecurring { get; set; }

        /// <summary>
        /// 分期付款长度
        /// </summary>
        public int RecurringCycleLength { get; set; }

        /// <summary>
        /// 分期付款周期
        /// </summary>
        public virtual RecurringProductCyclePeriod RecurringCyclePeriod { get; set; }

        /// <summary>
        /// 总共需要分期付款次数
        /// </summary>
        public int RecurringTotalCycles { get; set; }
        #endregion

        #region 运送运费
        /// <summary>
        /// 是否需要运送
        /// </summary>
        public bool IsShipEnabled { get; set; }

        /// <summary>
        /// 是否免运费
        /// </summary>
        public bool IsFreeShipping { get; set; }

        /// <summary>
        /// 附加运费
        /// </summary>
        public decimal AdditionalShippingCharge { get; set; }

        /// <summary>
        /// 重量(kg)
        /// </summary>
        public decimal Weight { get; set; }

        /// <summary>
        /// 长度(cm)
        /// </summary>
        public decimal Length { get; set; }

        /// <summary>
        /// 宽度(cm)
        /// </summary>
        public decimal Width { get; set; }

        /// <summary>
        /// 高度(cm)
        /// </summary>
        public decimal Height { get; set; }
        #endregion

        #region 税
        /// <summary>
        /// 是否免税
        /// </summary>
        public bool IsTaxExempt { get; set; }

        /// <summary>
        /// 计税分类Id
        /// </summary>
        public int TaxCategoryId { get; set; }
        #endregion

        #region 库存
        /// <summary>
        /// 库存管理方式
        /// </summary>
        public ManageInventoryMethod ManageInventoryMethodId { get; set; }

        /// <summary>
        /// 库存数量
        /// </summary>
        public int StockQuantity { get; set; }

        /// <summary>
        /// 是否显示库存
        /// </summary>
        public bool DisplayStockAvailability { get; set; }

        /// <summary>
        /// 是否显示库存数量
        /// </summary>
        public bool DisplayStockQuantity { get; set; }

        /// <summary>
        /// 最低库存
        /// </summary>
        public int MinStockQuantity { get; set; }

        /// <summary>
        /// 当库存达到最低库存时处理方式
        /// </summary>
        public LowStockActivity LowStockActivity { get; set; }

        /// <summary>
        /// 最低库存为多少时通知管理员
        /// </summary>
        public int NotifyAdminForQuantityBelow { get; set; }

        /// <summary>
        /// 库存策略
        /// </summary>
        public BackorderMode BackorderMode { get; set; }

        /// <summary>
        /// 当库存不够时是否添加到BackInStockSubscriptions
        /// </summary>
        public bool AllowBackInStockSubscriptions { get; set; }
        #endregion

        /// <summary>
        /// 订单中可购买最小数量
        /// </summary>
        public int OrderMinimumQuantity { get; set; }

        /// <summary>
        /// 订单中可购买最大数量
        /// </summary>
        public int OrderMaximumQuantity { get; set; }

        /// <summary>
        /// 禁用添加到购物车或购买按钮
        /// </summary>
        public bool DisableBuyButton { get; set; }

        /// <summary>
        /// 禁用收藏按钮
        /// </summary>
        public bool DisableWishlistButton { get; set; }

        /// <summary>
        /// 使用"联系我"代替价格
        /// </summary>
        public bool CallForPrice { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 原价
        /// </summary>
        public decimal OldPrice { get; set; }

        /// <summary>
        /// 成本
        /// </summary>
        public decimal ProductCost { get; set; }

        /// <summary>
        /// 特殊价格
        /// </summary>
        public decimal? SpecialPrice { get; set; }

        /// <summary>
        /// 特殊价格开始时间
        /// </summary>
        public DateTime? SpecialPriceStartDateTime { get; set; }

        /// <summary>
        /// 特殊价格结束时间
        /// </summary>
        public DateTime? SpecialPriceEndDateTime { get; set; }

        #region 用户输入价格
        /// <summary>
        /// 是否可接受用户输入价格
        /// </summary>
        public bool CustomerEntersPrice { get; set; }

        /// <summary>
        /// 用户可输入最低价格
        /// </summary>
        public decimal MinimumCustomerEnteredPrice { get; set; }

        /// <summary>
        /// 用户可输入最高价格
        /// </summary>
        public decimal MaximumCustomerEnteredPrice { get; set; }
        #endregion

        /// <summary>
        /// 产品生效可用于
        /// </summary>
        public DateTime? AvailableStartDateTime { get; set; }

        /// <summary>
        /// 产品过期不可用于
        /// </summary>
        public DateTime? AvailableEndDateTime { get; set; }

        /// <summary>
        /// 显示排序
        /// </summary>
        public int DisplayOrder { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public ProductStatus Status { get; set; }

        /// <summary>
        /// 全名称
        /// </summary>
        public string FullProductName
        {
            get
            {
                var product = Product;
                if (product != null)
                {
                    if (!string.IsNullOrWhiteSpace(Name))
                        return product.Name + " (" + Name + ")";
                    return product.Name;
                }
                return string.Empty;
            }
        }         
         */

    }
}